import {
  asyncRoutes,
  constantRoutes
} from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission (roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes (routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = {
      ...route
    }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncMenus (routes, menus) {
  const res = []
  routes.forEach(route => {
    if (route.hidden) {
      res.push({
        ...route
      })
    } else {
      const exist = menus.filter(item => item.path === route.path.replace(/\//, ''))
      if (exist.length) {
        const tmp = {
          ...route
        }
        if (route.children && route.children.length) {
          tmp.redirect = route.path + '/' + exist[0].children[0].path
          tmp.children = filterAsyncMenus(route.children, exist[0].children)
        }
        res.push(tmp)
      }
    }
  })
  return res
}

const state = {
  routes: [],
  addRoutes: [],
  tabTag: ''
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_TAB_TAG: (state, tabTag) => {
    state.tabTag = tabTag
  }
}

const actions = {
  // generateRoutes ({
  //   commit
  // }, roles) {
  //   return new Promise(resolve => {
  //     let accessedRoutes
  //     if (roles.includes('superadmin')) {
  //       accessedRoutes = asyncRoutes || []
  //     } else {
  //       accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
  //     }
  //     commit('SET_ROUTES', accessedRoutes)
  //     resolve(accessedRoutes)
  //   })
  // },
  generateRoutes ({
    commit
  }, data) {
    return new Promise(resolve => {
      let accessedRoutes
      if (data.roleArray.includes('superadmin')) {
        const index = asyncRoutes.findIndex(item => item.name === 'Order')
        asyncRoutes.splice(index, 1)
        accessedRoutes = asyncRoutes || []
      } else if (data.roleArray.includes('instructor')) {
        const hasRouters = asyncRoutes.filter(item => item.name === 'Order' || item.path === '*')
        accessedRoutes = hasRouters || []
      } else {
        // accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
        accessedRoutes = filterAsyncMenus(asyncRoutes, data.menus)
      }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  },
  generateTabTag ({
    commit
  }, tag) {
    return new Promise(resolve => {
      commit('SET_TAB_TAG', tag)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
